home *** CD-ROM | disk | FTP | other *** search
/ Atari Forever 4 / Atari Forever 4.zip / Atari Forever 4.iso / FALCON / DEMOSRGB / FD_RGB06 / U_TRACK / EXEMPLE / PRGFLAGS.DOC < prev    next >
Text File  |  1998-03-14  |  7KB  |  188 lines

  1. (Preliminary) documentation for memory protection in MiNT:
  2.  
  3. Memory protection works on a per-process basis.  Unless it uses the special
  4. mode bits in Mxalloc, a process' entire text+data+bss and any memory it
  5. allocates will be of the same type.  There are four types of memory:
  6.  
  7.     1. Private.  Only the process itself (and the OS) can use the memory.
  8.  
  9.     2. Global.  This memory is totally unprotected.
  10.  
  11.     3. Super.  This memory can be accessed by anybody from Super mode.
  12.  
  13.     4. Private/readable.  Anybody can read, nobody else can write.
  14.  
  15. By default, all processes load into private memory.  TSRs won't work in
  16. this kind of memory, but lots of other programs will.  A TSR should have
  17. its PRGFLAGS set to load into global or super memory.  There are other
  18. programs which need to grant wider access to their memory space: Gulam is
  19. one, because Gulam's children can call Gulam through a pointer found in the
  20. system variable space.  But Gulam is a bad choice of shell anyway, because
  21. you can't run more than one of them.  Current versions of STeno also must
  22. grant wider access, because it passes data to STalker using shared memory;
  23. private/readable is adequate for that.
  24.  
  25. Programs that load before MiNT does (i.e. in the AUTO folder) will find
  26. themselves in global memory.  This is the easiest way to be sure that
  27. your TSRs will go on working: RAMdisks, FOLDR100, CACHE080, etc. all
  28. work when loaded that way.
  29.  
  30. Notice that a process can grant wider access to its own address space, but
  31. it can't change the access permissions of any other process' space. If you
  32. set the PRGFLAGS of some process so it loads into global memory, that
  33. doesn't mean it can access anybody else's memory, it just means that
  34. anybody can access ITS memory.
  35.  
  36. ----------------------------------------------------------------------------
  37.  
  38. PRGFLAGS for protection:
  39.  
  40.     The second-least-significant nybble of PRGFLAGS is the default mode
  41. for your program's memory protection.
  42.  
  43.     0x00: private memory.  Only you and the AES can touch it.
  44.     0x10: Global: anybody can touch it.
  45.     0x20: super: anybody who is supervisor can touch it.
  46.     0x30: private/readable: anybody can read, nobody else can write.
  47.  
  48.     Other values are reserved for Atari use and/or future modes.
  49.  
  50. You can use PRGFLAGS.PRG to change the PRGFLAGS value for any program. From
  51. the shell, say (for example) "prgflags 0xf0 0x10 myprog.prg" to set
  52. "myprog.prg" to "global" (0x10).  The way you use PRGFLAGS is to give it
  53. two numbers, then the program name(s) to operate on.  The first number is
  54. taken as a mask, and the second as a value: bits which are one in the mask
  55. will be set to the corresponding bits in the value; bits which are zero in
  56. the mask will not be changed.  Thus the example sets bits 4-7 to "0001"
  57. and leaves all other bits alone.
  58.  
  59. You can specify the protection you want on a region when you Malloc it:
  60. call Mxalloc(size,mode), where the least-significant nybble of "mode"
  61. means what it always has, and the next four bits are the desired
  62. protection mode:
  63.  
  64.     0x00    default (whatever's in your PRGFLAGS)
  65.     0x10    private
  66.     0x20    global
  67.     0x30    super
  68.     0x40    readable
  69.  
  70.     0x4000    NO-FREE (see below)
  71.  
  72. ----------------------------------------------------------------------------
  73.  
  74. You can use Fopen on u:\proc\*.<pid> to get a file handle to a process.
  75. Then you can use Fcntl to get and set things about that process.
  76.  
  77.     #include <osbind.h>
  78.     #include <mintbind.h>
  79.     #include <filesys.h>    /* for PBASEADDR and other constants */
  80.     #include <uproc.h>    /* for struct PROCESS and other things */
  81.  
  82.     long baseaddr;
  83.     long ctxtsize;
  84.     long procaddr;
  85.     long flags;
  86.     char buf[14];
  87.     int fd;
  88.     PROCESS proc;        /* structure from uproc.h */
  89.     CONTEXT ctxt;
  90.  
  91.     sprintf(buf,"u:\\proc\\*.%03d",(int)Pgetpid());
  92.     fd = Fopen(buf,2);
  93.  
  94.     Fcntl(fd,&baseaddr,PBASEADDR);
  95.         Puts the basepage address of the process in arg.
  96.  
  97.     Fcntl(fd,&ctxtsize,PCTXTSIZE)
  98.         Puts the size of a MiNT context at arg.
  99.  
  100.     Fcntl(fd,&procaddr,PPROCADDR)
  101.         Puts the address of the MiNT process structure for the process
  102.         in arg.  To read the process structure do this:
  103.  
  104.         Fseek(procaddr,fd,0);
  105.         Fread(fd,sizeof(proc),&proc);
  106.  
  107.         To read the user context (registers, etc.) do this:
  108.  
  109.         Fseek(procaddr-ctxtsize,fd,0);
  110.         Fread(fd,sizeof(ctxt),&ctxt);
  111.  
  112.         To read any part of the memory owned by the process, do this:
  113.  
  114.         Fseek(addr,fd,0);
  115.         Fread(fd,size,buffer);
  116.  
  117.         This Fread will only work if the region you want to read is in
  118.         fact owned by the process that 'fd' refers to.  If the process
  119.         terminates, then the handle becomes invalid.
  120.  
  121.         If you Fopen process zero (MiNT itself) then you can Fseek to
  122.         any address that is managed by MiNT and read or write there.
  123.         This is for debuggers and similarly dangerous tools.
  124.  
  125.     Fcntl(fd,&flags,PGETFLAGS)
  126.         Puts the low 16 bits of a process' PRGFLAGS into 'flags'.
  127.  
  128.     Fcntl(fd,&flags,PSETFLAGS)
  129.         Sets the low 16 bits of a process' PRGFLAGS from 'flags'.
  130.  
  131. ----------------------------------------------------------------------------
  132.  
  133. To review, here are all the defined bits in the PRGFLAGS field of PRG file
  134. headers, and what they mean:
  135.  
  136.     BITS    MEANING
  137.     31-28    This number, plus one, times 128K, is the minimum amount
  138.         of alternative RAM that is acceptable.  Used when there
  139.         is more ST RAM than alternative RAM.
  140.  
  141.     27-14    Reserved; currently unused.
  142.  
  143.     15    Reserved for the OS. All programs should use zero here.
  144.  
  145.     14-8    Reserved; currently unused.
  146.  
  147.     7-4    This is the default memory-protection mode:
  148.         0=private, 1=global, 2=super, 3=world-readable.
  149.  
  150.     3    Reserved; currently unused.
  151.  
  152.     2    When 1, Malloc calls may be satisfied from alternative RAM.
  153.  
  154.     1    When 1, the program may load into alternative RAM.
  155.  
  156.     0    When 1, the program loads faster: the heap is not cleared.
  157.  
  158. And in Mxalloc's "mode" argument...
  159.  
  160.           BITS    MEANING
  161.  
  162.         ALTERNATIVE RAM ELIGIBILITY
  163.     0-2    0: ST RAM only
  164.         1: Alternative RAM only
  165.         2: Either, ST RAM preferred
  166.         3: Either, alternative RAM preferred
  167.  
  168.     3:    When set, means "change the protection mode of a region to
  169.         the protection-mode field."  You may only do this if you
  170.         own the region; in this case the first argument to Mxalloc
  171.         is the starting address of the region, as returned from an
  172.         earlier Mxalloc or Malloc or Pexec (modes 3 and 5) call.
  173.  
  174.         PROTECTION MODE
  175.     4-7    0: Default (from your PRGFLAGS)
  176.         1: Private
  177.         2: Global
  178.         3: Super
  179.         4: World-readable
  180.         other values are undefined and reserved.
  181.  
  182.     14    NO-FREE MODE
  183.         When set, this bit means "if the owner of this region
  184.         terminates, don't free this region. Instead, let MiNT
  185.         inherit it, so it'll never be freed."  This is a special
  186.         mode meant for the OS only, and may not remain available
  187.         to user processes.
  188.